﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ScheduleFriendly : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // security part
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            OnlineUser ou = (OnlineUser)Session["User"];
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();

            if (ou.TeamId == "Null")
                Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");
            
            if (!IsPostBack)
            {
                this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ScheduleFriendly_PageTitle;
                //this.Title = Resources.Resource.ScheduleFriendly_PageTitle + " - " + GND.ReturnAppTitle(); 
                
                TeamIdHiddenField.Value = ou.TeamId;
                if (ou.TeamId == "Null")
                    Response.Redirect("~/GeneralMessage.aspx?MessageString=M74ggdizz5wfeh");

                if (Request.QueryString["TeamId"] != null)
                {
                    InvitedTeamIdHiddenField.Value = Request.QueryString["TeamId"];
                    TeamToInviteLink.NavigateUrl = "TeamHomePage.aspx?TeamId=" + InvitedTeamIdHiddenField.Value;
                    TeamToInviteLink.Text = GND.ReadNivdalStringData("TeamId", "Nivdal_GetTeamNameByTeamId", InvitedTeamIdHiddenField.Value);
                }
                else
                    Response.Redirect("~/TeamHomePage.aspx");
            }
        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        //protected void FillDatesDDL()
        //{
        //    string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //    using (SqlConnection myConnection = new SqlConnection(ConnectionString))
        //    {
        //        DataSet ds = new DataSet();
        //        SqlCommand cmd = new SqlCommand("Nivdal_FrienlyGetListOfDatesWhenTeamsCanPlay");
        //        cmd.CommandType = CommandType.StoredProcedure;
        //        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        //        adp.Fill(ds);
        //        DateDDL.DataSource = ds;
        //        DateDDL.DataTextField = ConvertDate("Date");
        //        DateDDL.DataValueField = "Date";
        //        DateDDL.DataBind();
        //    }
        //}

        private string FormatDateForSQL(string Date)
        {
            string ReturnValue = "";
            try
            {
                DateTime t = DateTime.Parse(Date);
                ReturnValue = t.Year.ToString() + "-" +
                              t.Month.ToString() + "-" +
                              t.Day.ToString() + " " +
                              t.Hour.ToString() + ":" +
                              t.Minute.ToString();
            }
            catch
            { }
            return ReturnValue;
        }

        private string FormatDate(object Date)
        {
            string ReturnValue = Date.ToString();
            try
            {
                ReturnValue = string.Format("{0:d}", Date);
            }
            catch { ReturnValue = "error"; }
            return (ReturnValue);
        }

        protected void DateChanged(object sender, EventArgs e)
        {
            // after date is cnahned - hours DDL is populated
            string Date = FormatDateForSQL(DateDDL.SelectedValue);
            if (Date != "0")
            {
                HourDataSource.SelectParameters.Clear();
                HourDataSource.SelectParameters.Add("Team1", TeamIdHiddenField.Value);
                HourDataSource.SelectParameters.Add("Team2", InvitedTeamIdHiddenField.Value);
                HourDataSource.SelectParameters.Add("Date", Date);
                HourDataSource.DataBind();
                HoursPanel.Visible = true;
                MinutesPanel.Visible = false;
                SendInvitationPanel.Visible = false;
                HourDDL.DataBind();
            }
        }

        protected void HourChanged(object sender, EventArgs e)
        {
            string Date = FormatDateForSQL(HourDDL.SelectedValue);
            if (Date != "0")
            {
                MinutesDataSource.SelectParameters.Clear();
                MinutesDataSource.SelectParameters.Add("Team1", TeamIdHiddenField.Value);
                MinutesDataSource.SelectParameters.Add("Team2", InvitedTeamIdHiddenField.Value);
                MinutesDataSource.SelectParameters.Add("Date", Date);
                MinutesDataSource.DataBind();
                MinutesPanel.Visible = true;
                SendInvitationPanel.Visible = false;
                MinutesDropDownList.DataBind();
            }
        }

        protected void MinuteChanged(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string Date = FormatDateForSQL(MinutesDropDownList.SelectedValue);
                if (Date != "0")
                {
                    SendInvitationPanel.Visible = true;
                    InvitationConfirmTitlelLabel.Text = Resources.Resource.ScheduleFriendly_InvitationConfirm + " " + MinutesDropDownList.SelectedValue;

                }
                
            }
        }

        protected void SendInvitation(object sender, EventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Answer = GND.ReadNivdalStringData("@SuggestingUser", "@ChalangedTeam", "@Time",
                                                     "@SuggestingTeamHosts", "@CupRules", "Nivdal_FriendlySuggestFriendly",
                                                     ou.UserId, this.InvitedTeamIdHiddenField.Value,
                                                     FormatDateForSQL(this.MinutesDropDownList.SelectedValue),
                                                     this.HomeAwayCheckBoxList.SelectedValue,
                                                     this.CupRulesCheckBoxList.SelectedValue);
            Response.Redirect("Friendly.aspx?Message=" + Answer);
        }

        protected void DayRender(object sender, DayRenderEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            // TODO!!!
        }
    }
}